package com.readbook.service;

import com.readbook.dao.LoginDao;
import com.readbook.dao.UaccountDao;
import com.readbook.dao.WechartDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginService {
    @Autowired
    private LoginDao loginDao;

    @Autowired
    private WechartDao wechartDao;

    @Autowired
    private UaccountDao uaccountDao;

    public Map<String, Object> onlogin(Map<String, Object> gmap) {
        Map<String, Object> smap = new HashMap<String, Object>();
        //1.查询用户名是否存在
        List<Map<String, Object>> list1 = loginDao.GetlistByUname(gmap);
        if (list1.size() > 0) {
            //2.如何用户名存在  则查询密码是否错误
            List<Map<String, Object>> list2 = loginDao.GetlistByUpwd(gmap);
            if (list2.size() > 0) {
                String usid = list2.get(0).get("USID").toString();
                List<Map<String, Object>> list3 = loginDao.getUserInfo(usid);
                smap = list3.get(0);
                smap.put("result", true);
            } else {
                smap.put("result", false);
                smap.put("msg", "密码错误！");
            }
        } else {
            smap.put("result", false);
            smap.put("msg", "用户名不存在！");
        }
        return smap;
    }


    public Map<String, Object> updateUaccount(Map<String, Object> gmap) {
        Map<String, Object> smap = new HashMap<String, Object>();
        //判断账号是否和其他账号重复
        List<Map<String, Object>> list1 = loginDao.GetlistByUnameUsid(gmap);
        if (list1.size() > 0) {
            smap.put("result", false);
            smap.put("msg", "账号已经存在，不能修改！");
            return smap;
        }
        //查询旧密码是否正确
        Map<String, Object> mmap = new HashMap<String, Object>();
        mmap.put("uname", gmap.get("olduname"));
        mmap.put("upwd", gmap.get("oupwd"));
        List<Map<String, Object>> list2 = loginDao.GetlistByUpwd(mmap);
        if (list2.size() <= 0) {
            smap.put("result", false);
            smap.put("msg", "旧密码错误！");
            return smap;
        }
        //修改
        int i = loginDao.updateUaccount(gmap);
        if (i > 0) {
            smap.put("result", true);
            return smap;
        } else {
            smap.put("result", false);
            smap.put("msg", "修改失败！");
            return smap;
        }
    }


    /**
     * @author 牟明艳
     * @comments 根据账号查询权限
     * @time 2019年10月26日18:26:05
     */
    public String getUserPermission(Map<String, Object> gmap) {
        String permission = loginDao.getUserPermission(gmap);
        return permission;
    }


    public List<Map<String, Object>> getUserInfoByZhMm(Map<String, Object> gmap) {
        List<Map<String, Object>> list1 = loginDao.getUserInfoByZhMm(gmap);
        return list1;
    }




    public List<Map<String, Object>> getUaccountByZhMm(Map<String, Object> gmap) {
        List<Map<String, Object>> list1 = loginDao.getUaccountByZhMm(gmap);
        return list1;
    }



    public List<Map<String, Object>> getUaccountBydlZh(String dlzh) {
        List<Map<String,Object>>teacher=uaccountDao.getTeaUaccountByzh(dlzh);
        return teacher;
    }


    public List<Map<String, Object>> getUaccountBydlZhName(Map<String, Object> gmap) {
        List<Map<String,Object>>teacher=loginDao.getUaccountBydlZhName(gmap);
        return teacher;
    }


    public int update_Uaccount(Map<String, Object> gmap) {
        return uaccountDao.updateUaccountByTeazh(gmap);
    }

    public int insert_Uaccount(Map<String, Object> gmap) {
        return uaccountDao.insertUaccount(gmap);
    }


}
